package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.DeviceModel;
import com.iot.manage.pojo.ModelArgs;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DeviceModelMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.MODEL_ID modelId,  \n" +
            " temp.MODEL_CODE modelCode,  \n" +
            " temp.MODEL_NAME modelName,\n" +
            " temp.MODEL_ARG modelArg\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_DEVICE_MODEL t\n" +
            "\tWHERE (t.MODEL_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.MODEL_CODE LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.MODEL_ARG LIKE CONCAT(CONCAT('%', #{query}),'%')) \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.MODEL_ID\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<DeviceModel> findDeviceModelList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.MODEL_ID) FROM T_DEVICE_MODEL a WHERE a.MODEL_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.MODEL_CODE LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.MODEL_ARG LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countDeviceModelList(String query);

    @Insert("<script>" +
            "INSERT INTO T_DEVICE_MODEL \n" +
            "(MODEL_ID,MODEL_CODE,MODEL_NAME" +
            "<if test=\" modelArg != null\">"+
            ",MODEL_ARG" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{modelId},#{modelCode},#{modelName}" +
            "<if test=\" modelArg != null\">"+
            ",#{modelArg}" +
            "</if>"+
            ")" +
            "</script>")
    void addDeviceModel(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.MODEL_ID modelId,  \n" +
            " temp.MODEL_CODE modelCode,  \n" +
            " temp.MODEL_NAME modelName,\n" +
            " temp.MODEL_ARG modelArg\n" +
            "FROM T_DEVICE_MODEL temp\n" +
            "\tWHERE temp.MODEL_ID = #{params} OR temp.MODEL_CODE = #{params}\n" +
            "</script>")
    DeviceModel findDeviceModelByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_DEVICE_MODEL SET \n" +
            "\tMODEL_CODE = #{modelCode},\n" +
            "\tMODEL_NAME = #{modelName}\n" +
            "<if test=\" modelArg != null\">"+
            "\t,MODEL_ARG = #{modelArg}\n" +
            "</if>"+
            "WHERE MODEL_ID = #{modelId}" +
            "</script>")
    void updateDeviceModelInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_DEVICE_MODEL WHERE MODEL_ID = #{modelId}</script>")
    void deleteDeviceModelById(String modelId);

    @Select("<script>SELECT a.DPROPERTY_CODE id,1 pid,a.DPROPERTY_NAME label FROM T_DEVICE_PROPERTY a " +
            "<if test=\" propertyParams != ''\">"+
            "WHERE a.DPROPERTY_CODE NOT IN (${propertyParams})" +
            "</if></script>")
    List<ModelArgs> findPropertyArgsList(@Param("propertyParams") String propertyParams);

    @Select("<script>SELECT a.DEVENT_CODE id,2 pid,a.DEVENT_NAME label FROM T_DEVICE_EVENT a " +
            "<if test=\" eventParams != ''\">"+
            "WHERE a.DEVENT_CODE NOT IN (${eventParams})</if></script>")
    List<ModelArgs> findEventArgsList(@Param("eventParams") String eventParams);

    @Select("<script>SELECT a.DSERVICE_CODE id,3 pid,a.DSERVICE_NAME label FROM T_DEVICE_SERVICE a " +
            "<if test=\" serviceParams != ''\">"+
            "WHERE a.DSERVICE_CODE NOT IN (${serviceParams})</if></script>")
    List<ModelArgs> findServiceArgsList(@Param("serviceParams") String serviceParams);

    @Update("<script>" +
            "UPDATE T_DEVICE_MODEL SET \n" +
            "\tMODEL_ARG = #{args}\n"+
            "WHERE MODEL_ID = #{modelId}" +
            "</script>")
    void updateDeviceModelArgById(JSONObject params);

    @Select("<script>SELECT a.MODEL_ARG args FROM T_DEVICE_MODEL a WHERE MODEL_ID = #{modelId}</script>")
    String findModelArgById(String modelId);

    @Select("<script>SELECT a.DPROPERTY_CODE id,1 pid,a.DPROPERTY_NAME label FROM T_DEVICE_PROPERTY a WHERE a.DPROPERTY_CODE IN (${propertyParams})</script>")
    List<ModelArgs> findPropertyArgsListYes(@Param("propertyParams") String propertyParams);

    @Select("<script>SELECT a.DEVENT_CODE id,2 pid,a.DEVENT_NAME label FROM T_DEVICE_EVENT a WHERE a.DEVENT_CODE IN (${eventParams})</script>")
    List<ModelArgs> findEventArgsListYes(@Param("eventParams") String eventParams);

    @Select("<script>SELECT a.DSERVICE_CODE id,3 pid,a.DSERVICE_NAME label FROM T_DEVICE_SERVICE a WHERE a.DSERVICE_CODE IN (${serviceParams})</script>")
    List<ModelArgs> findServiceArgsListYes(@Param("serviceParams") String serviceParams);
}
